package com.eliza.client.route

import com.eliza.client.model.User
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.requestvalidation.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.utils.io.*
import kotlinx.io.readByteArray
import kotlinx.serialization.json.Json


fun Application.configureSerialization() {
    install(ContentNegotiation) {
        json()
    }
    routing {
        install(RequestValidation) {
            validate<User> { customer ->
                if (customer.age <= 3)
                    ValidationResult.Invalid("A customer ID should be greater than 0")
                else ValidationResult.Valid
            }
            validate {
                filter { body ->
                    body is ByteArray
                }
                validation { body ->
                    check(body is ByteArray)
                    val intValue = String(body).toInt()
                    if (intValue <= 0)
                        ValidationResult.Invalid("A value should be greater than 0")
                    else ValidationResult.Valid
                }
            }
        }
        route("/req") {
            get("/query") {
                println(call.parameters)
            }
            post("/raw") {
//                println(call.receiveText())
            }
            post("/array") {
                val body = call.receive<ByteArray>()
                call.respond(String(body))
            }
            post("/binary") {
                val bytes = call.receive<ByteArray>()
                call.respondBytes(bytes)
            }
            post("/binary/sequences") {
                val readChannel = call.receiveChannel()
                val text = readChannel.readRemaining().readByteArray()
                call.respondBytes(text)
            }
            post("/form-data") {
// form-data
                println(call.receiveParameters())
            }
            post("/form-ulencoded") {
                println(call.receiveParameters())
            }
            get("/urlParam") {
                call.request.queryParameters.forEach { s, value ->
                    println("$s, $value")
                }
            }
            get("/path/{name}/{value}") {
                call.pathParameters.forEach { s, value ->
                    println("$s, $value")
                }
            }
            post("/readBody") {
                val receive = call.receive<String>()
                print(receive.toString())
            }
            post("/object") {
//              415  因為訊息的內容格式是不受支援的
                val receive = call.receive<User>()
                print(receive)
                call.respond(receive.toString())
            }

        }
    }
    routing {
        route("/resp") {
            get("/text") {
                call.respondText("text", ContentType.Application.Json)
            }
            get("/json") {
                val user = User("name", 12, arrayListOf(1, 2, 3))
                call.respondText(Json.encodeToString(user), ContentType.Application.Json)
            }
            get("/binary") {
                call.respondBytes("abcd".encodeToByteArray(), ContentType.Application.Json)
            }
            get("/object") {
            }
            get("/xml") {

            }
            get("/protobuf") {

            }
            get("/yaml") {

            }
        }
    }
}
